﻿using FluentFTP;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Baby.Shop.Util
{
    public static class FtpHelper
    {
        private static readonly FtpClient _ftpClient;

        static FtpHelper()
        {
            _ftpClient = new FtpClient("127.0.0.1")
            {
                Encoding = Encoding.UTF8,
                Credentials = new NetworkCredential("Anonymous", "")
            };
        }

        public static async Task<bool> UploadAsync(byte[] filebyte, string remotePath)
        {
            return await Task.Run(() =>
            {
                try
                {
                    _ftpClient.Connect();
                    var status = _ftpClient.UploadBytes(filebyte, remotePath, FtpRemoteExists.Overwrite, createRemoteDir: true);
                    return status == FtpStatus.Success;
                }
                finally
                {
                    _ftpClient.Disconnect();
                }
            });
        }

        public static async Task<bool> UploadAsync(Stream stream, string remotePath)
        {
            return await Task.Run(() =>
            {
                try
                {
                    _ftpClient.Connect();
                    var status = _ftpClient.UploadStream(stream, remotePath, createRemoteDir: true);
                    return status == FtpStatus.Success;
                }
                finally
                {
                    _ftpClient.Disconnect();
                }
            });
        }

        public static async Task<byte[]> DownloadAsync(string remotePath)
        {
            return await Task.Run(() =>
            {

                try
                {
                    _ftpClient.Connect();
                    using (var memory = new MemoryStream())
                    {
                        _ftpClient.DownloadStream(memory, remotePath);
                        return memory.ToArray();
                    }
                }
                finally
                {
                    _ftpClient.Disconnect();
                }

            });

        }

        public static async Task<MemoryStream> DownloadByByteAsync(string remotePath)
        {
            return await Task.Run(() =>
            {

                try
                {
                    _ftpClient.Connect();

                    byte[] bytes;
                    _ftpClient.DownloadBytes(out bytes, remotePath);
                    return new MemoryStream(bytes);
                }
                finally
                {
                    _ftpClient.Disconnect();
                }

            });

        }

        public static async Task<bool> FileExistsAsync(string remotePath)
        {
            return await Task.Run(() =>
            {

                try
                {
                    _ftpClient.Connect();
                    return _ftpClient.FileExists(remotePath);
                }
                finally
                {
                    _ftpClient.Disconnect();
                }

            });

        }
        public static void Dispose()
        {
            _ftpClient?.Dispose();
        }
    }


}
